package com.liz.lizrpccore.beans;


import com.liz.lizrpccore.beans.proxys.entrance.ServiceProxy;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author liangGongWei
 * @description
 * @create 2021-08-25 14:07
 **/

public class ResourceBean<T> implements FactoryBean<T> {

    private Class<T> interfaceType;

    public ResourceBean(Class<T> interfaceType){
        this.interfaceType = interfaceType;
    }


    @Override
    public T getObject() throws Exception {
        InvocationHandler handler = new ServiceProxy<>(interfaceType);
        return  (T) Proxy.newProxyInstance(interfaceType.getClassLoader(),new Class<?>[]{interfaceType},handler);
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceType;
    }
}
